package com.lzl.eduservice.controller;


import com.lzl.common_utils.R;
import com.lzl.eduservice.entity.EduChapter;
import com.lzl.eduservice.entity.chapter.ChapterVo;
import com.lzl.eduservice.service.EduChapterService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-03-30
 */
@RestController
@RequestMapping("/eduservice/chapter")

public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 课程大纲列表，根据课程id进行查询
     * @param courseId
     * @return
     */
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list = eduChapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo",list);
    }

    /**
     * 添加章节
     * @param eduChapter
     * @return
     */
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    /**
     * 根据章节id查询
     * @param chapterId
     * @return
     */
    @GetMapping("getChapter/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId){
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return R.ok().data("eduChapter",eduChapter);

    }

    /**
     * 修改章节
     * @param eduChapter
     * @return
     */
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    /**
     * 删除章节的方法
     * 如果章节里面没有小节，直接删除
     * 如果章节里面有小节
     *      第一种：删除章节的时候，把章节里面所有的小节都删除
     *      第二种：如果删除的章节下面有小节，不让进行删除---需要把小节删了才能删
     *      现在用第二种
     * @param chapterId
     * @return
     */
    @DeleteMapping("deleteChapter/{chapterId}")
    public R deleteChapterById(@PathVariable String chapterId){
       Boolean b = eduChapterService.deleteChapter(chapterId);
       if(b){
           return R.ok();
       }else {
           return R.error();
       }

    }





}

